package com.atguigu.lease.web.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service

public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private  LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private  GraphInfoService graphInfoService;
    @Autowired
    private  RoomInfoService roomInfoService;
    @Autowired
    private  PaymentTypeService paymentTypeService;
    @Autowired
    private  LeaseTermService leaseTermService;
    @Override
    public List<AgreementItemVo> selectAgreementItemVo(Long userId) {
        return leaseAgreementMapper.selectAgreementItemVo(userId);
    }
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        AgreementDetailVo res=new AgreementDetailVo();
        //获得租约信息
        LeaseAgreement leaseAgreementServiceById =Db.lambdaQuery(LeaseAgreement.class)
                        .eq(LeaseAgreement::getId,id)
                                .one();
        BeanUtils.copyProperties(leaseAgreementServiceById,res);
        //获得 公寓名称
        Long apartmentId = leaseAgreementServiceById.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(apartmentId);
        res.setApartmentName(apartmentInfo.getName());
        //获取公寓图片列表
        List<GraphInfo> graphInfos = graphInfoService.lambdaQuery()
                .eq(GraphInfo::getItemId, apartmentInfo.getId())
                .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .list();
        List<GraphVo> graphVos = BeanUtil.copyToList(graphInfos, GraphVo.class);
        res.setApartmentGraphVoList(graphVos);
        //获取房间的号
        Long roomId = leaseAgreementServiceById.getRoomId();
        RoomInfo roomInfo = roomInfoService.getById(roomId);
        res.setRoomNumber(roomInfo.getRoomNumber());
        //获取房间的图片
        List<GraphInfo> graphInfosRoom = graphInfoService.lambdaQuery()
                .eq(GraphInfo::getItemType, ItemType.ROOM)
                .eq(GraphInfo::getItemId, roomInfo.getId())
                .list();
        List<GraphVo> graphVos1 = BeanUtil.copyToList(graphInfosRoom, GraphVo.class);
        res.setRoomGraphVoList(graphVos1);
        //获取支付方式
        PaymentType paymentType = paymentTypeService.lambdaQuery()
                .eq(PaymentType::getId, leaseAgreementServiceById.getPaymentTypeId())
                .one();
        res.setPaymentTypeName(paymentType.getName());
        //获取租期数据
        LeaseTerm leaseTerm = leaseTermService.lambdaQuery()
                .eq(LeaseTerm::getId, leaseAgreementServiceById.getLeaseTermId())
                .one();
        res.setLeaseTermUnit(leaseTerm.getUnit());
        res.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        return res;
        //return leaseAgreementMapper.getDetailById(id);
    }
}




